﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RightsRegistrationWeb.Default"
    EnableSessionState="True" %>

<html>
<head id="Head1" runat="server">
    <link rel="Stylesheet" href="CSS/Right.css" />
    <title>Cadastro de Direitos</title>

    <script type="text/javascript">
        function ResetFormForInsert() {
            rightsForm.hdnCodigoDireito.value = "";
            rightsForm.hdnSelected.value = "";
            rightsForm.txtDescricao.value = "";
            rightsForm.cbTipoDireito.selectedIndex = 0;
            rightsForm.txtDescricao.focus();
            ResetGridColor();

            var table = document.getElementById('tblModules');

            for (i = 0; i < table.rows.length; i++) {
                for (j = 0; j < table.rows[i].cells.length; j++) {
                    if (table.rows[i].cells[j].children.length > 0) {
                        var chk = table.rows[i].cells[j].children[0];
                        chk.checked = false;
                    }
                }
            }
        }

        function ConfirmDeletion() {
            if (confirm("Você tem que certeza que deseja deletar o(s) item(s) selecionado(s)?")) {
                rightsForm.hdnDeleteAction.value = "S";
                rightsForm.submit();
            }
        }

        function GridItemClick(dataGrid, linha, modules) {

            grid = eval(dataGrid);
            var rows = grid.getElementsByTagName("tr");

            for (i = 0; i < (rows.length - 1); i++) {
                rows[i].bgColor = "#ffffff";
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox")
                        inputs[j].checked = false;
                }
            }

            linha = eval(linha);
            linha.style.backgroundColor = "#B2BAC6";
            //linha.getElementsByTagName("input")[0].checked = true;

            tds = linha.getElementsByTagName("td");
            codigoDireito = tds[1].firstChild.data;
            //descricaoDireito = tds[2].firstChild.data;
            descricaoDireito = tds[2].childNodes[0].firstChild.data;
            codigoTipoDireito = linha.codigotipodireito;
            codigoTipoOperacao = linha.codigotipooperacao;

            SetForm(codigoDireito, descricaoDireito, codigoTipoDireito, codigoTipoOperacao, modules);

            //rightsForm.btDeleteSelected.disabled = false;
            rightsForm.cbTipoDireito.disabled = true;

        }

        function GetSelectedItems(grid) {
            rightsForm.txtDescricao.value = "";
            rightsForm.cbTipoDireito.selectedIndex = 0;
            rightsForm.cbTipoOperacao.selectedIndex = 0;
            rightsForm.hdnSelected.value = "";

            var rows = grid.getElementsByTagName("tr");

            for (i = 1; i < rows.length; i++) {
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox" && inputs[j].checked) {
                        rightsForm.hdnSelected.value += inputs[j].id + ",";
                    }
                }
            }

            rightsForm.hdnSelected.value = rightsForm.hdnSelected.value.substring(0, rightsForm.hdnSelected.value.length - 1);

            if (rightsForm.hdnSelected.value.length == 0)
                rightsForm.btDeleteSelected.disabled = true;
            else
                rightsForm.btDeleteSelected.disabled = false;
        }

        function SetForm(codigoDireito, descricaoDireito, codigoTipoDireito, codigoTipoOperacao, modules) {
            rightsForm.hdnSelected.value = "";
            rightsForm.hdnCodigoDireito.value = codigoDireito;
            //rightsForm.txtDescricao.value = descricaoDireito.replace("http://www.bovespa.com.br/", "");
            rightsForm.txtDescricao.value = descricaoDireito;

            for (i = 0; i < rightsForm.cbTipoDireito.length; i++) {
                if (rightsForm.cbTipoDireito[i].value == codigoTipoDireito) {
                    rightsForm.cbTipoDireito.selectedIndex = i;
                    break;
                }
            }

            for (i = 0; i < rightsForm.cbTipoOperacao.length; i++) {
                if (rightsForm.cbTipoOperacao[i].value == codigoTipoOperacao) {
                    rightsForm.cbTipoOperacao.selectedIndex = i;
                    break;
                }
            }

            var table = document.getElementById('tblModules');
            for (i = 0; i < table.rows.length; i++) {
                for (j = 0; j < table.rows[i].cells.length; j++) {
                    if (table.rows[i].cells[j].children.length > 0) {
                        var chk = table.rows[i].cells[j].children[0];
                        chk.checked = false;
                    }
                }
            }

            var module = modules.split(',');
            for (i = 0; i < module.length; i++) {
                var chk = document.getElementById(module[i]);
                if (chk != null)
                    chk.checked = true;
            }
        }

        function ResetGridColor() {
            grid = eval("grdRights");
            var rows = grid.getElementsByTagName("tr");

            for (i = 1; i < rows.length; i++) {
                inputs = rows[i].getElementsByTagName("input");
                for (j = 0; j < inputs.length; j++) {
                    if (inputs[j].type == "checkbox") {
                        inputs[j].checked = false;
                        rows[i].bgColor = "#ffffff";
                    }
                }
            }
        }
    </script>

</head>
<body style="margin-left: 0px; margin-top: 0px; margin-bottom: 0px; margin-right: 0px;">
    <form id="rightsForm" runat="server">
    <asp:ScriptManager runat="server" />
    <input type="hidden" id="hdnDeleteAction" name="hdnDeleteAction" runat="server" />
    <input type="hidden" id="hdnCodigoDireito" name="hdnCodigoDireito" runat="server" />
    <input type="hidden" id="hdnSelected" name="hdnSelected" runat="server" />
    <table width="100%" border="0">
        <tr>
            <td>
                <fieldset>
                    <legend>Formulário de Cadastro</legend>
                    <asp:UpdatePanel runat="server">
                        <ContentTemplate>
                            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                <tr valign="middle" style="height: 30px;">
                                    <td width="100" valign="middle">
                                        Descrição:
                                    </td>
                                    <td valign="middle">
                                        <asp:TextBox ID="TextBox1" Text="http://www.bovespa.com.br/" Width="200" Enabled="false"
                                            runat="server" />
                                        <asp:TextBox ID="txtDescricao" runat="server" Width="650" />
                                    </td>
                                </tr>
                                <tr valign="middle" style="height: 30px;">
                                    <td colspan="2">
                                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                            <tr>
                                                <td width="100">
                                                    Tipo do Direito:
                                                </td>
                                                <td width="400">
                                                    <asp:DropDownList ID="cbTipoDireito" runat="server" />
                                                </td>
                                                <td width="100">
                                                    Tipo da Operação:
                                                </td>
                                                <td>
                                                    <asp:DropDownList ID="cbTipoOperacao" runat="server" />
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr style="height: 25px;">
                                    <td valign="middle" width="100">
                                        Módulos:
                                    </td>
                                    <td valign="middle">
                                        <table cellpadding="0" cellspacing="0" border="0" id="tblModules" runat="server" />
                                    </td>
                                </tr>
                                <tr style="height: 30px;">
                                    <td>
                                        &nbsp;
                                    </td>
                                    <td>
                                        <asp:Button ID="btSave" runat="server" Text="Salvar" Width="80px" />
                                    </td>
                                </tr>
                            </table>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset>
                    <legend>Pesquisa</legend>
                    <table cellpadding="0" cellspacing="0" border="0" width="100%">
                        <tr valign="middle">
                            <td width="100" valign="middle">
                                Pesquisa:
                            </td>
                            <td valign="middle" width="320">
                                <asp:TextBox ID="txtPesquisa" Text="" Width="300" runat="server" />
                            </td>
                            <td style="text-align: left;">
                                <asp:UpdatePanel runat="server">
                                    <ContentTemplate>
                                        <asp:Button runat="server" ID="btnPesquisar" Text="Pesquisar" OnClick="btnPesquisar_Click" OnClientClick="ResetFormForInsert();" />
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <table width="100%">
                    <tr valign="top">
                        <td width="1">
                            <asp:Button ID="btDeleteSelected" runat="server" Text="Deletar itens selecionados"
                                Enabled="false" onmousedown="ConfirmDeletion()" />
                        </td>
                        <td width="1">
                            <input type="button" id="btNewItem" value="Inserir novo item" onclick="ResetFormForInsert()" />
                        </td>
                        <td width="99%" align="right">
                            <asp:UpdatePanel runat="server">
                                <ContentTemplate>
                                    <asp:Label ID="lblMessage" runat="server" />
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <asp:UpdatePanel runat="server" UpdateMode="Always">
                    <ContentTemplate>
                        <asp:GridView runat="server" AllowPaging="True" PageSize="8" AllowSorting="True"
                            CellPadding="2" CellSpacing="1" ForeColor="#333333" Font-Size="X-Small" GridLines="None"
                            Width="100%" ID="grdRights" AutoGenerateColumns="False" OnPageIndexChanging="grdRights_PageIndexChanging"
                            OnRowDataBound="grdRights_RowDataBound" OnSorting="grdRights_Sorting">
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Center" Wrap="False" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#999999" />
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <Columns>
                                <asp:TemplateField HeaderText="&nbsp;" HeaderStyle-Width="25px" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <input type="checkbox" id="<%# Eval("CodigoDireito") %>" onclick="GetSelectedItems(<%= grdRights.ClientID %>);" />
                                    </ItemTemplate>
                                    <HeaderStyle Width="25px" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="Codigo" DataField="CodigoDireito" HeaderStyle-Width="55px"
                                    ItemStyle-HorizontalAlign="Center">
                                    <HeaderStyle Width="55px" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:BoundField>
                                <asp:TemplateField HeaderText="Descrição" HeaderStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:LinkButton runat="server" ID="lnkDescricaoDireito" href="#" />
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Left" />
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="Tipo Direito" DataField="DescricaoTipoDireito" HeaderStyle-HorizontalAlign="Left">
                                    <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:TemplateField HeaderText="Tipo Operação" HeaderStyle-HorizontalAlign="Center"
                                    HeaderStyle-Width="50" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Label runat="server" ID="lblGrdTipoOperacao" />
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Center" Width="90px" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
